package helpers;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.metamodel.EntityType;

public class DomainModelLister implements IDomainModelLister 
{
	 private EntityManagerFactory emf;
	 
	 public DomainModelLister() 
	 {
	     emf = Persistence.createEntityManagerFactory("EmiFrameWorkPU");
		           
	 }
	
	/* (non-Javadoc)
	 * @see helpers.IDomainModelLister#returnAllDomainModelNames()
	 */
	@Override
	public String returnAllDomainModelNames()
	{
		EntityManager entityManager = emf.createEntityManager();
		
		List<String> ModelNames = new ArrayList<String>();
		
		for (EntityType<?> entity : entityManager.getMetamodel().getEntities()) 
			ModelNames.add(entity.getJavaType().getName());
		
		String ModelNamesXML = SerializeModelNames(ModelNames);
	
		return ModelNamesXML;
	
	}
	
	private String SerializeModelNames(List<String> ModelNames)
	{
		String GeneratedXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
		GeneratedXML +="<modelnames> \n";
		for (String Name : ModelNames)
			GeneratedXML += "<name>"+Name+"</name> \n";
			
			GeneratedXML += "</modelnames>";
			
		return GeneratedXML;
	}
}